// page/component/new-pages/cart/cart.js
Page({
  data: {
    carts: [], // 购物车列表
    hasList: false, // 列表是否有数据
    totalPrice: 0, // 总价，初始为0
    selectAllStatus: false, // 全选状态，默认全选
    buyQty: 1
  },
  onShow() {
    //wx.setStorageSync('car', []);
    var arr = wx.getStorageSync('car') || [];
    var list = [];
    var i = 1;
    for (let p of arr) {
      list.push({
        id: p.id,
        name: p.name,
        image: '/images/2a.jpg',
        num: p.num,
        price: p.price,
        selected: false
      });
      i++;
    }
    this.setData({
      hasList: true,
      carts: list
    });
    this.getTotalPrice();
  },
  /**
   * 当前商品选中事件
   */
  selectList(e) {
    const index = e.currentTarget.dataset.index;
    let carts = this.data.carts;
    const selected = carts[index].selected;
    carts[index].selected = !selected;
    this.setData({
      carts: carts
    });
    this.getTotalPrice();
  },

  /**
   * 删除购物车当前商品
   */
  deleteList(e) {
    const index = e.currentTarget.dataset.index;
    let carts = this.data.carts;
    carts.splice(index, 1);
    this.setData({
      carts: carts
    });
    if (!carts.length) {
      this.setData({
        hasList: false
      });
    } else {
      this.getTotalPrice();
    }
  },

  /**
   * 购物车全选事件
   */
  selectAll(e) {
    let selectAllStatus = this.data.selectAllStatus;
    selectAllStatus = !selectAllStatus;
    let carts = this.data.carts;

    for (let i = 0; i < carts.length; i++) {
      carts[i].selected = selectAllStatus;
    }
    this.setData({
      selectAllStatus: selectAllStatus,
      carts: carts
    });
    this.getTotalPrice();
  },

  /**
   * 绑定加数量事件
   */
  addCount(e) {
    const index = e.currentTarget.dataset.index;
    let carts = this.data.carts;
    let num = carts[index].num;
    num = num + 1;
    carts[index].num = num;
    this.setData({
      carts: carts
    });
    this.getTotalPrice();
  },

  /**
   * 绑定减数量事件
   */
  minusCount(e) {
    const index = e.currentTarget.dataset.index;
    const obj = e.currentTarget.dataset.obj;
    let carts = this.data.carts;
    let num = carts[index].num;
    if (num <= 1) {
      return false;
    }
    num = num - 1;
    carts[index].num = num;
    this.setData({
      carts: carts
    });
    this.getTotalPrice();
  },

  /**
   * 计算总价
   */
  getTotalPrice() {
    let carts = this.data.carts; // 获取购物车列表
    let total = 0;
    for (let i = 0; i < carts.length; i++) { // 循环列表得到每个数据
      if (carts[i].selected) { // 判断选中才会计算价格
        total += carts[i].num * carts[i].price; // 所有价格加起来
      }
    }
    this.setData({ // 最后赋值到data中渲染到页面
      carts: carts,
      totalPrice: total.toFixed(2)
    });
  },

  orderConfirm: function(e) {
    var cars = this.data.carts;
    var orderLines = [];
    //这里调用微信支付，结算，并生成订单，保存到数据库中，后面进行订单状态的追踪
    for (let i = 0; i < cars.length; i++) { // 循环列表得到每个数据
      if (cars[i].selected) { // 判断选中才会计算价格
        orderLines.push({
          id: cars[i].id,
          num: cars[i].num,
          price: cars[i].price,
          name: cars[i].name,
          image: cars[i].image
        })
      }
    }
    if (orderLines == null || orderLines.length == 0) {
      wx.showToast({
        title: '至少选择一件!',
        icon: 'success',
        duration: 2000
      })
      return;
    }

    var orderInfo = {
      orderLines: orderLines,
      totalPrice: this.data.totalPrice
    }
    var orderInfo = JSON.stringify(orderInfo);
    wx.navigateTo({
      //在接收页面的url后面加上“？自定义名称=字符串”就可以通过url传值
      url: '../orderConfirm/orderConfirm?orderInfo=' + orderInfo
    })
  },
  addQty: function(e) {
    var qty = parseInt(e.currentTarget.dataset.num) + 1;
    var index = e.currentTarget.dataset.index;
    this.data.carts[index].num = qty;
    this.setData({
      carts: this.data.carts
    })
  },
  subQty: function(e) {
    var qty = parseInt(e.currentTarget.dataset.num) - 1;
    var index = e.currentTarget.dataset.index;
    this.data.carts[index].num = qty;
    this.setData({
      carts: this.data.carts
    })
  },
})